import request from "@/utils/request";

/**
 * 获取资源列表
 * @param {Object} params - 查询参数
 * @param {number} params.pageNum - 页码
 * @param {number} params.pageSize - 每页大小
 * @param {number} params.applicationId - 应用ID
 * @param {string} params.name - 资源名称
 * @param {string} params.code - 资源编码
 * @param {string} params.type - 资源类型
 * @param {number} params.parentId - 父级ID
 * @param {string} params.status - 资源状态
 * @returns {Promise}
 */
export const getResourceList = (params) => {
  return request({
    url: "/api/system/ae/resources",
    method: "get",
    params,
  });
};

/**
 * 获取资源详情
 * @param {number} id - 资源ID
 * @returns {Promise}
 */
export const getResourceDetail = (id) => {
  return request({
    url: `/api/system/ae/resources/${id}`,
    method: "get",
  });
};

/**
 * 创建资源
 * @param {Object} data - 资源数据
 * @param {number} data.applicationId - 应用ID
 * @param {number} data.parentId - 父级ID
 * @param {string} data.name - 资源名称
 * @param {string} data.code - 资源编码
 * @param {string} data.type - 资源类型
 * @param {string} data.path - 路径
 * @param {string} data.method - 请求方法
 * @param {number} data.visible - 是否可见
 * @param {number} data.sort - 排序值
 * @param {string} data.remark - 备注
 * @returns {Promise}
 */
export const createResource = (data) => {
  return request({
    url: "/api/system/ae/resources",
    method: "post",
    data,
  });
};

/**
 * 更新资源
 * @param {number} id - 资源ID
 * @param {Object} data - 资源数据
 * @returns {Promise}
 */
export const updateResource = (id, data) => {
  return request({
    url: `/api/system/ae/resources/${id}`,
    method: "put",
    data,
  });
};

/**
 * 删除资源
 * @param {number} id - 资源ID
 * @returns {Promise}
 */
export const deleteResource = (id) => {
  return request({
    url: `/api/system/ae/resources/${id}`,
    method: "delete",
  });
};

/**
 * 批量删除资源
 * @param {Array<number>} ids - 资源ID数组
 * @returns {Promise}
 */
export const batchDeleteResource = (ids) => {
  return request({
    url: "/api/system/ae/resources/batch",
    method: "delete",
    data: { ids },
  });
};

/**
 * 获取资源树形结构
 * @param {number} applicationId - 应用ID
 * @returns {Promise}
 */
export const getResourceTree = (appId) => {
  return request({
    url: "/api/system/ae/resources/tree",
    method: "get",
    params: { appId },
  });
};

/**
 * 获取应用列表
 * @returns {Promise}
 */
export const getApplicationCodes = () => {
  return request({
    url: "/api/system/ae/resources/applications",
    method: "get",
  });
};

/**
 * 批量删除资源
 */
export const batchDeleteResources = (ids) => {
  return request({
    url: "/api/system/ae/resources/batch",
    method: "delete",
    data: { ids },
  });
};

/**
 * 启用资源
 * @param {number} id - 资源ID
 * @returns {Promise}
 */
export const enableResource = (id) => {
  return request({
    url: `/api/system/ae/resources/${id}/enable`,
    method: "post",
  });
};

/**
 * 禁用资源
 * @param {number} id - 资源ID
 * @returns {Promise}
 */
export const disableResource = (id) => {
  return request({
    url: `/api/system/ae/resources/${id}/disable`,
    method: "post",
  });
};

/**
 * 上传资源树
 * @param {string} applicationCode - 应用编码
 * @param {Array} resources - 资源数据数组
 * @returns {Promise}
 */
export const uploadResourceTree = (appCode, resources) => {
  return request({
    url: "/api/system/ae/resources/batch-upload",
    method: "post",
    data: {
      appCode,
      resources,
    },
  });
};
